<?php

/**
 * 模型对应数据库表 "web_guestbook".
 *
 * 以下是可用表web_guestbook的栏位:
 * @property string $id
 * @property string $site_key
 * @property string $username
 * @property string $email
 * @property string $title
 * @property string $content
 * @property string $add_time
 * @property string $ip
 * @property string $re_content
 * @property string $re_username
 * @property string $re_time
 * @property integer $state
 */
class WebGuestbook extends CActiveRecord {

    /**
     * 返回指定的基于“ar”的静态模型类.
     * @return WebGuestbook the static model class
     * @author nirsh
     * @example
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * 返回数据库表的名.
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function tableName() {
        return 'web_guestbook';
    }

    
        /*
     * 状态
     */
    public $status_array = array(
        1 => '审核通过',
        0 => '审核不通过',
    );
    
    
       /**
     * 获取审核状态
     * @param type $state
     * @return type 
     */
    public function getDelayState($state) {
        switch ($state) {
            case 0:
                $color = "red";
                break;
            case 1:
                $color = "greens";
                break;
            default :
                $color = "";
        }
        return "<font color='" . $color . "'>" . $this->status_array[$state] . "</font>";
    }
    
    


    /**
     * 返回验证规则
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function rules() {
        return array(
            array('site_key, username, content, add_time, ip, re_content, state', 'required'),
            array('state', 'numerical', 'integerOnly' => true),
            array('site_key, username, re_username', 'length', 'max' => 100),
            array('email, title', 'length', 'max' => 255),
            array('ip', 'length', 'max' => 50),
            array('re_time', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, site_key, username, email, title, content, add_time, ip, re_content, re_username, re_time, state', 'safe', 'on' => 'search'),
        );
    }

    /**
     * 返回关联规则数组。
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * 通过ID获取名称
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function getNameById($id, $nameField) {
        $res = $this->findByPk($id);
        return $res[$nameField] ? $res[$nameField] : '';
    }
    
    /**
     * 返回属性标签(名称= >标签)数组
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function attributeLabels() {
        return array(
            'id' => '系统编号',
            'username' => '用户名',
            'email' => '邮箱',
            'title' => '标题',
            'content' => '内容',
            'add_time' => '添加时间',
            'ip' => 'ip地址',
            'site_key' => '域名',
            're_content' => '回复内容',
            're_username' => '回复人',
            're_time' => '回复时间',
            'state' => '状态',
        );
    }

    /**
     * 设置查询相关条件
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id, true);
        $criteria->compare('site_key', $this->site_key, true);
        $criteria->compare('username', $this->username, true);
        $criteria->compare('email', $this->email, true);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('content', $this->content, true);
        $criteria->compare('add_time', $this->add_time, true);
        $criteria->compare('ip', $this->ip, true);
        $criteria->compare('re_content', $this->re_content, true);
        $criteria->compare('re_username', $this->re_username, true);
        $criteria->compare('re_time', $this->re_time, true);
        $criteria->compare('state', $this->state);

        return new CActiveDataProvider(get_class($this), array(
                    'criteria' => $criteria,
                ));
    }

}